package br.edu.ufcg.splab.techniques.reduction.requirement;

import java.util.HashSet;
import java.util.Set;

import br.edu.ufcg.splab.core.InterfaceEdge;
import br.edu.ufcg.splab.generator.TestCase;
import br.edu.ufcg.splab.generator.TestSuite;

//NETO: Feb 20th 2014
public class SingleTransitionsTracerGenerator implements InterfaceRequirementTracerGenerator {
	
	/**
	 * This method have the role of extract the transition pairs (requirements) from the test suite passed as 
	 * parameter.  
	 * @param root The root of the model that the suite was extracted.
	 * @param suite The suite to be related with the requirements.
	 * @return The tracer of the requirements to the test cases that satisfy them.
	 */	
	public RequirementTracer getTracer(TestSuite suite) {
		RequirementTracer tracer = new RequirementTracer();
		Set<String> requirements = getTransitionsAsReq(suite);
		
		//For each found requirement, the actual object is created and then the
		// test cases satisfying the current requirement is added to the tracer.
		for(String transitionReq: requirements) {
			InterfaceRequirement req = new Requirement();
			req.setLabel(transitionReq);
			for (TestCase test : suite.getTestSuite()) {
				if(satistfiesRequirement(test, transitionReq)){
					tracer.addTestCaseAndReq(test, req);
				}
			}
			tracer.addRequirement(req);
		}
		tracer.setTypeRequirement(this.getTypeRequirementTracer());
		return tracer;
	}
	
	public String getTypeRequirementTracer(){
		return "all-transitions";
	}
			
	/**
	 * Provide the transition pairs of the graph.
	 * 
	 * @param testSuite The test suite
	 * @param root The root of the graph measured.
	 * @return Returns all transition pairs of the graph.
	 */
	private Set<String> getTransitionsAsReq(TestSuite testSuite){
		Set<String> str = new HashSet<String>();
		for(TestCase tc : testSuite.getTestSuite()){
			for(InterfaceEdge transition : tc.getTestCase()){
				if(!str.contains(transition.getLabel())){
					str.add(transition.getLabel());
				}
			}			
		}
		return str;
	}
	
	/**
	 * Returns <tt>true</tt> if the test case contains the specified transition pair. 
	 * 
	 * @param testCase The test case for evaluation.
	 * @param requirement The transition pair whose presence in this test case is to be tested
	 * @return <tt>true</tt> if this list contains the specified transition pair. 
	 */
	private boolean satistfiesRequirement(TestCase testCase, String requirement) {	
		for(InterfaceEdge transition : testCase.getTestCase()){
			if(transition.getLabel().equals(requirement)){
				return true;
			}
		}
		return false;
	}	
}
